package restaurant.gui;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

/**
 * This JPanel is used to add or edit a menu item.
 * 
 * @author Odessa
 */
@SuppressWarnings("serial")
public class MenuItemPanel extends JPanel
{
	/** This panel. */
	private JPanel thisPanel = this;

	/** The item number text field. */
	private JTextField itemNoField;

	/** The name text field. */
	private JTextField nameField;

	/** The description text field. */
	private JTextField descField;

	/** The price text field. */
	private JTextField priceField;

	/** The preparation time text field. */
	private JTextField prepTimeField;

	/** The course combo box. */
	private JComboBox<String> courseComboBox;

	/** The check box to specify item is vegetarian. */
	private JCheckBox vegCheckBox;

	/** The option of whether the offer is being added or edited. */
	private int option;



	/**
	 * Creates the panel.
	 */
	public MenuItemPanel() {
		initalize();
		option = ActionsPanel.ADD;
	}



	/**
	 * Creates the panel and inputs the values.
	 *
	 * @param id the item number.
	 * @param name the name of the item.
	 * @param desc the item's description.
	 * @param price the price of the item.
	 * @param prepTime the preparation time of the item.
	 * @param course the course.
	 * @param veg the vegetarian item or not.
	 */
	public MenuItemPanel(int id, String name, String desc, double price,
			int prepTime, String course, boolean veg) {
		initalize();
		option = ActionsPanel.EDIT;

		itemNoField.setText(Integer.toString(id));
		nameField.setText(name);
		descField.setText(desc);
		priceField.setText(Double.toString(price));
		prepTimeField.setText(Integer.toString(prepTime));
		courseComboBox.setSelectedItem(course);
		vegCheckBox.setSelected(veg);

	}



	/**
	 * Initialize the components on the panel.
	 */
	private void initalize()
	{
		// Create the labels
		JLabel itemNoLabel = new JLabel("Item number");
		JLabel nameLabel = new JLabel("Name");
		JLabel descLabel = new JLabel("Description");
		JLabel priceLabel = new JLabel("Price");
		JLabel prepTimeLabel = new JLabel("Preparation time");
		JLabel courseLabel = new JLabel("Course");
		JLabel vegLabel = new JLabel("Vegetarian");

		// Create the text fields
		nameField = new JTextField();
		descField = new JTextField();
		priceField = new JTextField();
		prepTimeField = new JTextField();
		itemNoField = new JTextField();
		itemNoField.setEnabled(false);
		itemNoField.setColumns(5);

		String[] courseOptions = { "Drinks", "Entree", "Main Course", "Dessert" };
		courseComboBox = new JComboBox<>(courseOptions);
		courseComboBox.setSelectedIndex(0);

		vegCheckBox = new JCheckBox();

		// Create the buttons and their action listener.
		JButton saveButton = new JButton("Save");
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				saveButtonClicked(e);
			}
		});

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				JFrame frame = (JFrame) SwingUtilities.getRoot(thisPanel);
				frame.dispose();
			}
		});

		// Add buttons to panel.
		JPanel buttonPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) buttonPanel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);

		// Create group layout for this panel.
		GroupLayout layout = new GroupLayout(this);
		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												Alignment.TRAILING, false)
												.addComponent(
														buttonPanel,
														Alignment.LEADING,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addGroup(
														Alignment.LEADING,
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING)
																				.addComponent(
																						nameLabel)
																				.addComponent(
																						itemNoLabel)
																				.addComponent(
																						descLabel)
																				.addComponent(
																						priceLabel)
																				.addComponent(
																						courseLabel)
																				.addComponent(
																						prepTimeLabel)
																				.addComponent(
																						vegLabel))
																.addGap(10)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.TRAILING,
																				false)
																				.addComponent(
																						priceField,
																						Alignment.LEADING)
																				.addComponent(
																						descField,
																						Alignment.LEADING)
																				.addComponent(
																						prepTimeField,
																						Alignment.LEADING)
																				.addComponent(
																						itemNoField,
																						Alignment.LEADING,
																						GroupLayout.PREFERRED_SIZE,
																						46,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						nameField,
																						Alignment.LEADING,
																						GroupLayout.DEFAULT_SIZE,
																						245,
																						Short.MAX_VALUE)
																				.addComponent(
																						vegCheckBox,
																						Alignment.LEADING,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						courseComboBox,
																						Alignment.LEADING,
																						GroupLayout.DEFAULT_SIZE,
																						283,
																						Short.MAX_VALUE))))
								.addContainerGap(29, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(itemNoLabel)
												.addComponent(
														itemNoField,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(nameLabel)
												.addComponent(
														nameField,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(descLabel)
												.addComponent(
														descField,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(priceLabel)
												.addComponent(
														priceField,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(prepTimeLabel)
												.addComponent(
														prepTimeField,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(courseLabel)
												.addComponent(
														courseComboBox,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING, false)
												.addComponent(
														vegLabel,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														vegCheckBox,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addGap(7)
								.addComponent(buttonPanel,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(18, Short.MAX_VALUE)));
		layout.linkSize(SwingConstants.HORIZONTAL, new Component[] { nameField,
				descField, priceField, prepTimeField, courseComboBox });

		setLayout(layout);
	}



	/**
	 * Update button clicked.
	 * 
	 * @param e the e
	 */
	protected void saveButtonClicked(ActionEvent e)
	{
		int item = Integer.parseInt(itemNoField.getText());
		String name = nameField.getText();
		String description = descField.getText();
		double price = Double.parseDouble(priceField.getText());
		int prepTime = Integer.parseInt(prepTimeField.getText());
		String course = (String) courseComboBox.getSelectedItem();
		boolean vegetarian = vegCheckBox.isSelected();

		 if (option == ActionsPanel.ADD) {
		 Restaurant.actions.addMenuItem(name, description, price, prepTime,
		 course, vegetarian);
		 } else if (option == ActionsPanel.EDIT) {
		 Restaurant.actions.updateMenuItem(item, name, description, price,
		 prepTime, course, vegetarian);
		 }
                 
		 Frame frame =(Frame) SwingUtilities.getRoot(this);
                 frame.dispose();
	}
}
